package models

import (
	"crypto/md5"
	"fmt"
	"io"
	"mime/multipart"
	"os"
	"path"
	"strings"
)

func MD5(str string) string {
	data := []byte(str)
	has := md5.Sum(data)
	md5str1 := fmt.Sprintf("%x", has) //将[]byte转成16进制

	fmt.Println(md5str1)
	return md5str1
}
func Image(f multipart.File, m *multipart.FileHeader, e error) string {
	if e != nil {
		return ""
	}
	PathStr := ".jpg.jpeg.png"
	if !(strings.Contains(PathStr, path.Ext(m.Filename))) {
		return "格式错误"
	}
	if m.Size > 2<<20 {
		return "图片过大"
	}
	file, err := os.OpenFile("./static/img/"+m.Filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
	if err != nil {
		return "服务器繁忙，图片上传失败"
	}
	io.Copy(file, f)
	return "http://127.0.0.1:8080/static/img/" + m.Filename
}
